package someTestExcemple.junitTest;

import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import static org.junit.Assert.assertEquals;

public class ShoppingCartTest {
    private ShoppingCart cart;

    @BeforeEach
    public void setUp() {
        cart = new ShoppingCart(); // 初始化购物车
    }

    @AfterEach
    public void tearDown() {
        cart = null; // 清除购物车对象
    }

    @Test
    public void testAddItem() {
        // 创建测试商品
        Item item1 = new Item("苹果", 5.99);
        Item item2 = new Item("香蕉", 3.99);

        // 添加商品到购物车
        cart.addItem(item1);
        cart.addItem(item2);


        // 验证商品数量
        assertEquals("购物车商品数量错误", 2, cart.getItemCount());

        // 验证总价
        double expectedTotal = 5.99 + 3.99;
        assertEquals("购物车总价计算错误", expectedTotal, cart.getTotalPrice(), 0.001);
    }
}
